// UI 工具函数与全局安装器（中文注释，变量/函数使用英文）
// 说明：避免依赖第三方 $uv，优先使用原生 uni API，提高各端兼容性

// 显示轻提示
export const showToast = (title, icon = 'none') => {
  if (!title) title = ''
  uni.showToast({
    title,
    icon,
    duration: 2000
  })
}

// 显示加载中
export const showLoading = (title = '加载中...') => {
  uni.showLoading({
    title,
    mask: true
  })
}

// 隐藏加载中
export const hideLoading = () => {
  try {
    uni.hideLoading()
  } catch (_) {}
}

// 路由跳转相关
export const navigateTo = (url) => {
  if (!url) return
  uni.navigateTo({ url })
}

export const redirectTo = (url) => {
  if (!url) return
  uni.redirectTo({ url })
}

export const reLaunch = (url) => {
  if (!url) return
  uni.reLaunch({ url })
}

// 安装到全局（app.config.globalProperties）
export const installUIHelpers = (app) => {
  if (!app || !app.config) return
  app.config.globalProperties.$showToast = showToast
  app.config.globalProperties.$showLoading = showLoading
  app.config.globalProperties.$hideLoading = hideLoading
  app.config.globalProperties.$navigateTo = navigateTo
  app.config.globalProperties.$redirectTo = redirectTo
  app.config.globalProperties.$reLaunch = reLaunch
}

export default {
  install: installUIHelpers
}
